package com.ruoyi.web.controller.his;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.his.domain.HisDrugs;
import com.ruoyi.his.domain.HisManufactor;
import com.ruoyi.his.service.IHisDrugsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author shebin
 * @site www.694840063@qq.com
 * @company 扬州绘鑫勤商贸有限公司
 * @create 2021-08-23 20:50
 */

@RestController
@RequestMapping("/his/drugs")
public class hisDrugsController extends BaseController {

    @Autowired
    private IHisDrugsService hisDrugsService;

    @Log(title = "药品信息管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('his:drugs:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<HisDrugs> util = new ExcelUtil<HisDrugs>(HisDrugs.class);
        List<HisDrugs> drugsList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = hisDrugsService.importUser(drugsList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    @GetMapping("/importTemplate")
    public AjaxResult importTemplate()
    {
        ExcelUtil<HisDrugs> util = new ExcelUtil<HisDrugs>(HisDrugs.class);
        return util.importTemplateExcel("用户数据");
    }

    /**
     * 获取药品信息列表
     * @param drugs
     * @return
     */
    @PreAuthorize("@ss.hasPermi('his:drugs:list')")
    @GetMapping("/list")
    public TableDataInfo list(HisDrugs drugs){
        startPage();
        List<HisDrugs> list = hisDrugsService.selectDrugsList(drugs);
        return getDataTable(list);
    }

    /**
     * 获取药品信息列表
     * @return
     */
    @GetMapping("/drugsManufactorList")
    public List<HisManufactor> drugsManufactorList(){
        List<HisManufactor> drugsManufactor = hisDrugsService.selectDrugsManufactorList();
        return drugsManufactor;
    }

    /**
     * 根据编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('his:drugs:query')")
    @GetMapping(value = "/{drugsId}")
    public AjaxResult getInfo(@PathVariable Long drugsId)
    {
        return AjaxResult.success(hisDrugsService.selectDrugsById(drugsId));
    }

    /**
     * 新增药品信息
     */
    @PreAuthorize("@ss.hasPermi('his:drugs:add')")
    @Log(title = "药品信息管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody HisDrugs drugs)
    {
        drugs.setCreateBy(getUsername());
        return toAjax(hisDrugsService.insertDrugs(drugs));
    }

    /**
     * 修改药品信息
     */
    @PreAuthorize("@ss.hasPermi('his:drugs:edit')")
    @Log(title = "药品信息管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody HisDrugs drugs)
    {
        drugs.setUpdateBy(getUsername());
        return toAjax(hisDrugsService.updateDrugs(drugs));
    }

    /**
     * 删除药品信息
     */
    @PreAuthorize("@ss.hasPermi('his:drugs:remove')")
    @Log(title = "药品信息管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{drugsIds}")
    public AjaxResult remove(@PathVariable Long[] drugsIds){
        return toAjax(hisDrugsService.deleteDrugsByIds(drugsIds));
    }

    @Log(title = "药品信息管理", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('his:drugs:export')")
    @GetMapping("/export")
    public AjaxResult export(HisDrugs drugs)
    {
        List<HisDrugs> list = hisDrugsService.selectDrugsList(drugs);
        ExcelUtil<HisDrugs> util = new ExcelUtil(HisDrugs.class);
        return util.exportExcel(list, "药品信息数据");
    }


}
